using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;

namespace My.IdlConverter.Core.Util
{
    public class ComplierHelper
    {
        private static IDictionary<string, int> _dicOverloadMethod = new Dictionary<string, int>();

        public static bool IsInterProperty(Method source, Method target)
        {
            Method setter = null;
            Method getter = null;
            if (source.Setter)
                setter = source;
            if (target.Setter)
                setter = target;
            if (source.Getter)
                getter = source;
            if (target.Getter)
                getter = target;

            if (setter == null || getter == null)
                return false;

            string setterName = setter.Name.Remove(0, 3);
            string getterName = getter.Name.Remove(0, 3);
            if (setterName.Trim().Equals(getterName.Trim()))
            {
                if (setter.Parameters.Count == 1 && getter.Parameters.Count == 0)
                {
                    if (setter.Parameters[0].Type.PureTypeString.Equals(getter.ReturnType.PureTypeString))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public static bool ChangeOverloadMethod(ref Method methodObj, IList<Method> methods)
        {
            try
            {
                int overloadCount = 0;

                foreach (Method nextMethod in methods)
                {
                    if (IsOverLoad(methodObj, nextMethod))
                    {
                        try
                        {
                            overloadCount = _dicOverloadMethod[methodObj.Name];
                            overloadCount++;
                        }
                        catch (Exception e)
                        {
                            Trace.Write(e.Message);
                            overloadCount = 1;
                            _dicOverloadMethod[methodObj.Name] = overloadCount;
                        }
                    }
                }
                if (overloadCount > 0)
                {
                    methodObj.Name = methodObj.Name + "_" + overloadCount.ToString();
                    return true;
                }
            }
            catch (Exception e)
            {
                Trace.Write(e.Message);
            }

            return false;
        }

        public static bool IsOverLoad(Method source, Method target)
        {
            try
            {
                return source.Name.Equals(target.Name);
                //if (!source.Name.Equals(target.Name))
                //    return false;

                //int srcParamCount = source.Parameters.Count;
                //int targetParamCount = target.Parameters.Count;
                //if ((srcParamCount == 0) && (targetParamCount == 0))
                //    return true;

                //if (source.Parameters.Count == target.Parameters.Count)
                //{
                //    for (int i = 0; i < srcParamCount; ++i)
                //    {
                //        if (!source.Parameters[i].Type.TypeString.Equals(target.Parameters[i].Type.TypeString))
                //        {
                //            return true;
                //        }
                //    }
                //}

                //return false;
            }
            catch (Exception e)
            {
                Trace.Write(e.Message);
                return false;
            }
        }
    }
}
